package com.mmall.service.impl;

import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

@Service("iCategoryService")
public class CategoryServiceimpl implements ICategoryService {

//    private Logger logger = (Logger) LoggerFactory.getLogger(CategoryServiceimpl.class);
    @Autowired
    private CategoryMapper categoryMapper;

    public ServerResponse<String> addCategory(String categoryName,Integer parentId){
        if (parentId==null|| StringUtils.isBlank(categoryName)){
            return ServerResponse.createByErrorMessage("添加品类参数错误");
        }
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(false);
        int count = categoryMapper.insert(category);
        if (count>0){
            return ServerResponse.createBySuccessMessage("创建品类成功");
        }
        return ServerResponse.createByErrorMessage("创建品类失败");
    }

    public ServerResponse<String> updateCategoryName(Integer categoryId,String categoryName){
        if (categoryId==null|| StringUtils.isBlank(categoryName)){
            return ServerResponse.createByErrorMessage("添加品类参数错误");
        }
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);
        int count = categoryMapper.updateByPrimaryKeySelective(category);
        if (count>0){
            return ServerResponse.createBySuccessMessage("更新品类名称成功");
        }
        return ServerResponse.createByErrorMessage("更新品类名称失败");
    }

    public ServerResponse<List<Category>> seeCategoryList(Integer categoryId){
        List<Category> categoryList = categoryMapper.seeCategoryList(categoryId);
//        if (CollectionUtils.isEmpty(categoryList)){
//            logger.info("未找到当前分类");
//        }
        return ServerResponse.createBySuccess(categoryList);
    }

    public ServerResponse<List<Integer>> seeDeepCategoryList(Integer categoryId){
        Set<Category> category = new HashSet<Category>();
        seeCategoryChildList(category,categoryId);
        List<Integer> list= new ArrayList<Integer>();
        for (Category categoryItem:category){
            list.add(categoryItem.getId());
        }
        return ServerResponse.createBySuccess(list);
    }

    private Set<Category> seeCategoryChildList(Set<Category> category, Integer categoryId){
        Category category1 = categoryMapper.selectByPrimaryKey(categoryId);
        if (category1!=null){
            category.add(category1);
        }
        List<Category> categoryList = categoryMapper.seeCategoryList(categoryId);
        for (Category categoryItem:categoryList){
            seeCategoryChildList(category,categoryItem.getId());
        }
        return category;

    }
}
